﻿using JanusLibAing;

namespace VfQuestionsMy
{
    using System;
    using System.Windows.Forms;
    using Rsdn.Janus;
    using Rsdn.Janus.CoreServices.Extensibility;
    using Rsdn.Janus.CoreServices.NavigationTree;
    using Rsdn.Janus.CoreServices.NavigationTree.Extensibility;
    using Rsdn.SmartApp;

    [VirtualForumProvider]
    public class QuestionsMyProvider : IVirtualForumProvider
    {
        private readonly IServiceProvider provider;
        private ISharedInfoService sharedInfoService;

        private QuestionsMyItemInfo forumInfo = new QuestionsMyItemInfo();

        public QuestionsMyProvider(IServiceProvider provider)
        {
            this.provider = provider;
            if (provider == null)
            {
                throw new ArgumentNullException("provider");          //MLHIDE
            }

            sharedInfoService = provider.GetRequiredService<ISharedInfoService>();
        }

        #region Implementation of INavigationTreeProvider


        public Func<object, bool> IsUpdateNeedFunc
        {
            get { return IsUpdateNeed; }
        }

        public Action<int, MessageCounters> LoadCountersAction
        {
            get { return LoadCounters; }
        }

        public Func<bool, MessageCounters, IMsg> LoadTopicsAsChildrenFunc
        {
            get { return LoadTopicsAsChildren; }
        }

        public INavigationTreeItemInfo GetItemInfo(string itemId)
        {
            return forumInfo;
        }

        public Control CreateGuiControl()
        {
            return null;
        }

        #endregion

        public bool IsUpdateNeed(object msgIn)
        {
            bool ret = false;

            if (msgIn is Msg)
            {
                // don't move to class member we need actual value
                int selfId = sharedInfoService.GetIntValue(SharedInfoKeys.SelfId) ?? 0;

                Msg msg = msgIn as Msg;
                ret = msg.UserID == selfId;
            }

            return ret;
        }

        /// <summary>
        /// Loads the counters only.
        /// </summary>
        /// <param name="id"> The forum id. </param>
        /// <param name="messageCounters"> The message Counters. </param>
        internal void LoadCounters(int id, MessageCounters messageCounters)
        {
            QuestionsMyMessage root = new QuestionsMyMessage(ServiceProvider);
            using (new WaitCursor())
            {
                root.FillCounters();
            }
#if FRM_UPDATE_TRACE
            Trace.WriteLine("FRU:MyAnswers.LoadCounters");            //MLHIDE
#endif
            messageCounters.MessagesCount = root.RepliesCount; //here is wrong root.Count we don't have any loaded children;
            messageCounters.Unread = root.UnreadCount;
        }

        /// <summary>
        /// Loads all topics as children in message.
        /// </summary>
        /// <param name="isLoadAll">if set to <c>true</c> [is load all].</param>
        /// <returns>root message</returns>
        protected IMsg LoadTopicsAsChildren(bool isLoadAll, MessageCounters messageCounters)
        {
            QuestionsMyMessage root = new QuestionsMyMessage(ServiceProvider);
            using (new WaitCursor())
            {
                root.FillChildrenWithMessages();
#if FRM_UPDATE_TRACE
                Trace.WriteLine(
                    string.Format(
                        "FRM:My Questions:RepliesToMe Unread {0}, Unread {1}, summary {2}", //MLHIDE
                        root.RepliesToMeUnread,
                        root.Unread,
                        root.Count));
#endif
                messageCounters.MessagesCount = root.Count;
                messageCounters.Unread = root.UnreadCount; //root.RepliesUnread;
                messageCounters.RepliesToMeUnread = root.RepliesToMeUnread;
            }
            return root;
        }

        public IServiceProvider ServiceProvider
        {
            get { return provider; }
        }

    }
}

/*
[ExtensionStrategyFactory]
internal class CheckStateSourceStrategiesFactory : IExtensionStrategyFactory
     Class not defined                             - CheckStateSourceAttribute (janus)
           [CheckStateSource]
           internal sealed class NavigationCheckStateSource : CheckStateSource
           Janus.Navigation.NavigationWindowVisibility
internal class CommandStrategiesFactory : IExtensionStrategyFactory
     Class not defined - CommandTargetAttribute (Jet,MSsql,sqllite)
        [CommandTarget]
        internal sealed class JetCommandTarget : CommandTarget
        [CommandExecutor("Janus.Jet.CompactDb")]
        public void ExecuteCompactDb(ICommandContext context)
        [CommandStatusGetter("Janus.Jet.CompactDb")]
 	    public CommandStatus QueryCompactDbStatus(ICommandContext context)
	    [CommandTarget]
  	    internal sealed class MssqlCommandTarget : CommandTarget
	    [CommandExecutor("Janus.Mssql.CompactDb")]
     	    public void ExecuteCompactDb(ICommandContext context)
	    [CommandStatusGetter("Janus.Mssql.CompactDb")]
 	    public CommandStatus QueryCompactDbStatus(ICommandContext context)
        [CommandTarget]
        internal sealed class SqliteCommandTarget : CommandTarget
		        [CommandExecutor(_compactDbCmd)]
		        public void ExecuteCompactDb(ICommandContext context)
		        [CommandExecutor(_updateStatsCmd)]
		        public void ExecuteUpdateStatistics(ICommandContext context)
 		        [CommandStatusGetter(_compactDbCmd)]
		        [CommandStatusGetter(_updateStatsCmd)]
		        public CommandStatus QueryStatus(ICommandContext context)

    Class not defined - CommandProviderAttribute(Jet,MSsql,sqllite)
	    [CommandProvider]
            internal sealed class FirebirdCommandProvider : ResourceCommandProvider
            [CommandProvider]
	    internal sealed class FirebirdCommandProvider : ResourceCommandProvider
	    [CommandProvider]
	    internal sealed class SqliteCommandProvider : ResourceCommandProvider
internal class DataStrategiesFactory : IExtensionStrategyFactory
    DBDriverRegStrategy(publisher) - JanusDBDriverAttribute (all db)
            [JanusDBDriver(	DriverName,"Rsdn.Janus.Sqlite.Resources",	"DriverDisplayName","DriverDescription",LockRequired = true)]
	    internal class SqliteDriver : IDBDriver

internal class JanusExtStrategyFactory : IExtensionStrategyFactory
	ExtInfoProvidersStrategy(publisher) -  - ExtensionInfoProviderAttribute (all db)
 	    [ExtensionInfoProvider]
	    internal class FBExtensionInfoProvider : ExtensionInfoProviderBase <-- IExtensionInfoProvider
	    [ExtensionInfoProvider]
	    internal class JetExtensionInfoProvider : ExtensionInfoProviderBase <-- IExtensionInfoProvider

	StatsFormatterStrategy(publisher) - StatisticsFormatterAttribute (rsdn)
	ActivePartRegStrategy(publisher) - ActivePartAttribute (rsdn)
internal class MenuStrategiesFactory : IExtensionStrategyFactory
    MenuProviderStrategy - MenuProviderAttribute (Jet,MSsql,sqllite)
        [MenuProvider]
            internal sealed class MssqlMenuProvider : ResourceMenuProvider
                <menuCommand command="Janus.Jet.CompactDb"/>
        [MenuProvider]
            internal sealed class MssqlMenuProvider : ResourceMenuProvider
                <menuCommand command="Janus.Mssql.CompactDb"/>
        [MenuProvider]
        internal sealed class SqliteMenuProvider : ResourceMenuProvider
                <menuCommand command="Janus.Sqlite.CompactDb"/>
                <menuCommand command="Janus.Sqlite.UpdateStatistics"/>

internal class NavigationTreeStrategiesFactory : IExtensionStrategyFactory
    NavigationTreeProviderStrategy - NavigationTreeProviderAttribute - (not attached)
        NavigationTreeProviderInfo - Информация о провайдере элементов дерева навигации.
        NavigationTreeService
internal class SyncStrategiesFactory : IExtensionStrategyFactory
    Class is not defined - SyncProviderAttribute (rsdn)
	    [SyncProvider(JanusATInfo.ProviderName)]
	    internal class JanusATSyncProvider : WebServiceSyncProvider<JanusAT>

internal class TextMacrosStrategiesFactory : IExtensionStrategyFactory
    TextMacrosProviderStrategy(publisher) - TextMacrosProviderAttribute (janus)
        [TextMacrosProvider]
        internal sealed class CitationMacrosProvider : ITextMacrosProvider
	    [TextMacrosProvider]
	    internal sealed class LocalTimeMacrosProvider : ITextMacrosProvider
	    [TextMacrosProvider]
	    internal sealed class MediaPlayerMacrosProvider : ITextMacrosProvider
	    [TextMacrosProvider]
	    internal sealed class ResharperMacrosProvider : ITextMacrosProvider
	    [TextMacrosProvider]
	    internal sealed class SimpleMacrosProvider : ITextMacrosProvider
*/